03. Containerizing and Running Locally
Containerize the Flask App and Run Locally
The following steps describe how to complete the Dockerization part of the project. After you complete these steps, you should have the Flask application up and running in a Docker container.
-
If you haven't installed Docker already, you should install now using these installation instructions .
-
Create a Dockerfile named
Dockerfile
in the app repo. Your Dockerfile should:-
Use the
python:stretch
image as a source image - Set up an app directory for your code
- Install needed Python requirements
- Define an entrypoint which will run the main app using the Gunicorn WSGI server
Gunicorn should be run with the arguments as follows:
gunicorn -b :8080 main:APP
. -
Use the
-
Create a file named
env_file
and use it to set the environment variables which will be run locally in your container. Here, we do not need the export command, just an equals sign:<VARIABLE_NAME>=<VARIABLE_VALUE>
In this file, you should set both
JWT_SECRET
andLOG_LEVEL
, similar to how they were set as environment variables when you ran the Flask app locally. -
Build a local Docker image with the tag
jwt-api-test
. -
Run the image locally, using the Gunicorn server.
-
You can pass the name of the env file using the flag
--env-file=<YOUR_ENV_FILENAME>
. - You should expose the port 8080 of the container to the port 80 on your host machine.
-
You can pass the name of the env file using the flag
-
To use the endpoints, you can use the same curl commands as before, except using port 80 this time:
-
To try the
/auth
endpoint, use the following command:
export TOKEN=`curl -d '{"email":"<EMAIL>","password":"<PASSWORD>"}' -H "Content-Type: application/json" -X POST localhost:80/auth | jq -r '.token'`
-
To try the
/contents
endpoint which decrypts the token and returns its content, run:
curl --request GET 'http://127.0.0.1:80/contents' -H "Authorization: Bearer ${TOKEN}" | jq .
You should see the email that you passed in as one of the values.
-
To try the
Concept Checklist
Task Feedback:
Excellent work! Next, you will create the CD pipeline for the application.